package com.ztesoft.book.admin.controller.sys;

import com.ztesoft.book.admin.controller.base.BaseController;
import com.ztesoft.book.common.utils.Result;
import com.ztesoft.book.core.aliyun.oss.dto.STSResponse;
import com.ztesoft.book.core.aliyun.oss.service.STSService;
import com.ztesoft.book.core.sys.sysfile.dto.SysFileDTO;
import com.ztesoft.book.core.sys.sysfile.qo.SysFileQO;
import com.ztesoft.book.core.sys.sysfile.service.SysFileService;
import com.ztesoft.book.core.sys.sysuser.dto.SysUserDTO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 系统附件表
 *
 * @author 万书涵
 * 创建时间：2021-07-01
 */
@Slf4j
@RestController
@RequestMapping("/sys/sysFile")
public class SysFileController extends BaseController<SysFileService, SysFileDTO, SysFileQO> {

    @Resource
    private STSService stsService;

    @Value("${book.oss.accessKeyId}")
    private String accessKeyId;
    @Value("${book.oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${book.oss.bucket}")
    private String bucket;
    @Value("${book.oss.endPoint}")
    private String endPoint;
    @Value("${book.oss.region}")
    private String region;
    @Value("${book.oss.arm}")
    private String roleArn;

    @Resource
    private SysFileService sysFileService;

    @Override
    protected SysFileService getService() {
        return sysFileService;
    }

    /**
     * getTemOssAuth
     *
     * @return Result
     */
    @PostMapping("/getTemOssAuth")
    public @ResponseBody
    Result getTemOssAuth() {
        try {
            STSResponse stsResponse = stsService.getSTSCredentials(accessKeyId, accessKeySecret, roleArn,
                    "ossTem" + System.currentTimeMillis());
            stsResponse.setBucket(bucket);
            stsResponse.setEndPoint(endPoint);
            stsResponse.setRegion(region);
            String head = "https://" + bucket + "." + endPoint.replace("https://", "");
            stsResponse.setHead(head);
            return Result.success(stsResponse);
        }
        catch (Exception e) {
            log.error("获取临时权限失败", e);
            return Result.error("获取临时权限失败");
        }
    }

    /**
     * 文件直传oss后调用的接口,保存文件到sys_file
     */
    @PostMapping("/saveOssFile")
    @ApiOperation(value = "文件直传oss后调用的接口,保存文件到sys_file")
    public Result saveOssFile(@RequestBody SysFileDTO dto) {
        try {
            SysUserDTO user = getShiroUser();
            if (Objects.isNull(user)) {
                return Result.error("用户信息获取失败,请重新登录");
            }
            SysFileDTO sysFileDTO = sysFileService.saveOssFile(dto);
            return Result.success(sysFileDTO);
        }
        catch (Exception e) {
            log.error("文件上传失败", e);
            return Result.error("操作失败！");
        }
    }


}



